package com.ruoyi.partybuilding.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.partybuilding.common.EQueryController;
import com.ruoyi.partybuilding.common.PageSort;
import com.ruoyi.partybuilding.common.TableDataInfoRows;
import com.ruoyi.partybuilding.domain.MallGoods;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "商品管理")
@RestController
@RequestMapping("/partybuilding/MallGoods")
public class MallGoodsController extends EQueryController<MallGoods>
{
    /**
     * 查询列表
     */
    @Anonymous
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('partybuilding:MallGoods:list')")
    @GetMapping("/list")
    public TableDataInfoRows<MallGoods> list(MallGoods entity, PageSort page) {
        return super.list(entity, page);
    }



    /**
     * 获取详细信息
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('partybuilding:MallGoods:query')")
    @GetMapping(value = "/{id}")
    public R<MallGoods> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('partybuilding:MallGoods:add')")
    @Log(title = "商城商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Long> add(@RequestBody MallGoods entity)
    {
        if(StringUtils.isBlank(entity.getName())||entity.getPrice()==null)
            return R.bad("参数错误：名称及价格不能为空");
        var re=equeryable().where(p->p.eq(MallGoods::getName, entity.getName())).any();
        if(re)
            return R.bad("重复的商品名称");

        return super.add(entity);
    }

    /**
     * 修改
     */
    @ApiOperation("编辑")
    @PreAuthorize("@ss.hasPermi('partybuilding:MallGoods:edit')")
    @Log(title = "商城商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public R<Long> edit(@RequestBody MallGoods entity)
    {
        if(StringUtils.isBlank(entity.getName())||entity.getPrice()==null)
            return R.bad("参数错误：名称及价格不能为空");

        entity.setDeleted(null);
        return super.edit(entity);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('partybuilding:MallGoods:remove')")
    @Log(title = "商城商品", businessType = BusinessType.DELETE)
    @PostMapping("remove/{ids}")
    public R<Long> remove(@PathVariable Long[] ids)
    {
        return super.remove(ids);
    }
}
